package com.ruoyi.system.service.impl;


import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysMemberAddress;
import com.ruoyi.system.mapper.SysMemberAddressMapper;
import com.ruoyi.system.service.ISysMemberAddressService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 收货地址Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-25
 */
@Service
public class SysMemberAddressServiceImpl implements ISysMemberAddressService
{
    @Autowired
    private SysMemberAddressMapper sysMemberAddressMapper;

    /**
     * 查询收货地址
     *
     * @param id 收货地址主键
     * @return 收货地址
     */
    @Override
    public SysMemberAddress selectSysMemberAddressById(String id)
    {
        return sysMemberAddressMapper.selectSysMemberAddressById(id);
    }

    /**
     * 查询收货地址列表
     *
     * @param sysMemberAddress 收货地址
     * @return 收货地址
     */
    @Override
    public List<SysMemberAddress> selectSysMemberAddressList(SysMemberAddress sysMemberAddress)
    {
        return sysMemberAddressMapper.selectSysMemberAddressList(sysMemberAddress);
    }

    /**
     * 新增收货地址
     *
     * @param sysMemberAddress 收货地址
     * @return 结果
     */
    @Override
    public int insertSysMemberAddress(SysMemberAddress sysMemberAddress)
    {
        // 检查传入的收货地址是否为空
        if (sysMemberAddress == null) {
            throw new IllegalArgumentException("Address cannot be null");
        }
        // 查询是否存在默认地址
        // 创建一个新的SysMemberAddress对象用于查询
        SysMemberAddress param = new SysMemberAddress();
        // 设置查询条件，memberDefault为1，即查询默认地址
        param.setMemberDefault("1");
        // 设置查询条件，memberId为传入的收货地址的memberId
        param.setMemberId(sysMemberAddress.getMemberId());
        // 查询收货地址列表
        List<SysMemberAddress> sysMemberAddresses = sysMemberAddressMapper.selectSysMemberAddressList(param);

        // 判断查询结果是否为空
        if(ObjectUtils.isEmpty(sysMemberAddresses)){
            // 如果为空，则设置传入的收货地址为默认地址
            sysMemberAddress.setMemberDefault("1");
        }else{
            // 如果不为空，则设置传入的收货地址为非默认地址
            sysMemberAddress.setMemberDefault("0");
        }

        // 设置传入收货地址的创建时间为当前时间
        sysMemberAddress.setCreateTime(DateUtils.getNowDate());
        // 调用Mapper对象的方法，插入新的收货地址到数据库中
        return sysMemberAddressMapper.insertSysMemberAddress(sysMemberAddress);
    }

    /**
     * 修改收货地址
     *
     * @param sysMemberAddress 收货地址
     * @return 结果
     */
    @Override
    public int updateSysMemberAddress(SysMemberAddress sysMemberAddress) {
        if (sysMemberAddress == null) {
            throw new IllegalArgumentException("Address cannot be null");
        }
        if (sysMemberAddress.getMemberId() == null) {
            throw new IllegalArgumentException("MemberId cannot be null");
        }

        // 查询当前用户的默认地址信息
        SysMemberAddress queryParam = new SysMemberAddress();
        queryParam.setMemberDefault("1");
        queryParam.setMemberId(sysMemberAddress.getMemberId());
        List<SysMemberAddress> defaultAddresses = sysMemberAddressMapper.selectSysMemberAddressList(queryParam);

        // 处理默认地址逻辑
        if (ObjectUtils.isEmpty(defaultAddresses)) {
            sysMemberAddress.setMemberDefault("1");
        } else if ("1".equals(sysMemberAddress.getMemberDefault())) {
            // 取消其他默认地址
            for (SysMemberAddress defaultAddress : defaultAddresses) {
                defaultAddress.setMemberId(sysMemberAddress.getMemberId());
                defaultAddress.setMemberDefault("0");
                sysMemberAddressMapper.updateSysMemberAddress(defaultAddress);
            }
        }
        sysMemberAddress.setUpdateTime(DateUtils.getNowDate());
        return sysMemberAddressMapper.updateSysMemberAddress(sysMemberAddress);
    }



    /**
     * 批量删除收货地址
     *
     * @param ids 需要删除的收货地址主键
     * @return 结果
     */
    @Override
    public int deleteSysMemberAddressByIds(String[] ids)
    {
        return sysMemberAddressMapper.deleteSysMemberAddressByIds(ids);
    }

    /**
     * 删除收货地址信息
     *
     * @param id 收货地址主键
     * @return 结果
     */
    @Override
    public int deleteSysMemberAddressById(String id)
    {
        return sysMemberAddressMapper.deleteSysMemberAddressById(id);
    }
}
